package com.app.actions.basesetting;

import com.app.SpringAnnotationDef;
import com.app.actions.BaseAction;
import com.app.model.Goods_Info;
import com.app.model.Goods_Type;
import com.app.service.basesetting.GoodsService;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: cnprinces
 * Date: 13-1-24
 * Time: 下午4:24
 * To change this template use File | Settings | File Templates.
 */

@Scope(SpringAnnotationDef.SCOPE_PROTOTYPE)
@Component
@Namespace("/basesetting")
public class GoodsAction extends BaseAction {
    private static final Logger LOGGER = Logger.getLogger(GoodsAction.class);


    @Resource(name = SpringAnnotationDef.GOODS_SERVICE)
    private GoodsService service;


    /**
     * 商品类别
     */
    private Goods_Type goodsType;

    /**
     * 商品
     */
    private Goods_Info goodsInfo;


    @Action(value = "goods", results = {@Result(name = "menuInfo", location = "/basesetting/goods/getSystemCfg.jsp"),
            @Result(name = "addGoodsType", location = "/basesetting/goods/addGoodsType.jsp"),
            @Result(name = "addGoodsInfo", location = "/basesetting/goods/addGoodsInfo.jsp"),
            @Result(name = SUCCESS, location = "/basesetting/goods/list.jsp")})
    public String getGoods() {
        //获得所有的资源菜单
        List<Goods_Type> list = service.getDao().listAll();

        //往request里放attribute
        getRequest().setAttribute("list", list);

        //取得父节点               addresources
        List<Goods_Type> parentRes = new ArrayList<Goods_Type>();
        for (Goods_Type res : list) {
            if (res.getParentId() == 0) {
                parentRes.add(res);
            }
        }
        getRequest().setAttribute("parentRes", parentRes);

        return SUCCESS;
    }


    /**
     * 去到添加商品目录的页面
     *
     * @return
     */
    public String toAdd() {
        //获得所有的资源菜单
        List<Goods_Type> list = service.getDao().listAll();

        //往request里放attribute
        getRequest().setAttribute("list", list);

        //取得父节点               addresources
        List<Goods_Type> parentRes = new ArrayList<Goods_Type>();
        for (Goods_Type res : list) {
            if (res.getParentId() == 0) {
                parentRes.add(res);
            }
        }
        getRequest().setAttribute("parentRes", parentRes);


        if (goodsType != null) {
            goodsType = service.getDao().get(goodsType.getId());
            getRequest().setAttribute("goodsType", goodsType);
        }
        return "addGoodsType";
    }


    public String menuInfo() {
        goodsType = service.getDao().get(goodsType.getId());
        getRequest().setAttribute("goodsType", goodsType);

        // 获取他旗下的所有商品
        List<Goods_Info> goods_infos = service.loadGoodsInfoByType(goodsType);
        getRequest().setAttribute("goodsInfos", goods_infos);
        return "menuInfo";
    }

    public void delete() {
        goodsType = service.getDao().get(goodsType.getId());
        if (goodsType != null) {
            this.service.getDao().deleteObject(goodsType);
            JsonOut("删除成功！", "goodsType");
        } else {
            JsonOut("删除失败！", "goodsType");
        }
    }


    /**
     * 进行添加操作
     */
    public void add() {
        if (goodsType != null) {
            this.service.getDao().saveObject(goodsType);
            JsonOut("添加成功！", "goodsType");
        } else {
            JsonOut("添加失败！", "goodsType");
        }
    }


    /**
     * ******************************************************************************************************************
     * 商品添加
     * ******************************************************************************************************************
     *
     * @return
     */

    public String toAddGoodsInfo() {

        if (goodsInfo != null && goodsInfo.getId().intValue() != 0) {

            goodsInfo = service.getDao().getGoodsInfo(goodsInfo.getId());
            goodsType = service.getDao().get(goodsInfo.getTypeId());
            /*getRequest().setAttribute("goodsType", goodsType);*/
        }else {
            goodsType = service.getDao().get(goodsType.getId());
            /*getRequest().setAttribute("goodsType", goodsType);*/
        }
        return "addGoodsInfo";
    }

    public void addGoodsInfo() {
        if (goodsInfo != null) {
            goodsType = service.getDao().get(goodsType.getId());

            goodsInfo.setTypeId(goodsType.getId());
            goodsInfo.setTypeParentId(goodsType.getParentId());
            goodsInfo.setCreateDate(new Date());
            goodsInfo.setInputId(getSessionLoginedUser().getId());
            goodsInfo.setInputName(getSessionLoginedUser().getEname());
            service.getDao().saveObject(goodsInfo);
        }

        JsonOut("添加成功!", "closeCurrent");

    }

    public void delGoodsInfo() {
        if (goodsInfo != null) {
            goodsInfo = service.getDao().getGoodsInfo(goodsInfo.getId());

            service.getDao().deleteGoodsInfo(goodsInfo);

            JsonOut("删除成功!", "closeCurrent");
        }
    }


    public Goods_Type getGoodsType() {
        return goodsType;
    }

    public void setGoodsType(Goods_Type goodsType) {
        this.goodsType = goodsType;
    }


    public Goods_Info getGoodsInfo() {
        return goodsInfo;
    }

    public void setGoodsInfo(Goods_Info goodsInfo) {
        this.goodsInfo = goodsInfo;
    }
}
